#!/bin/bash

if [[ $# < 1 ]];then
    echo; echo "Usage: $0 pod_keywords <line> <namespace>"; echo;
    exit 1
fi

KEYWORDS=$1

LINES=${2:-1000}

NAMESPACE=${3:-__NAMESPACE}

KUBESERVER=__KUBE_MASTER

PODS=$(kubectl --server=${KUBESERVER} get pods --namespace=${NAMESPACE} | grep -i ${KEYWORDS} | cut -d' ' -f1)

PODSNUM=$(kubectl --server=${KUBESERVER} get pods --namespace=${NAMESPACE} | grep -i ${KEYWORDS} | cut -d' ' -f1 | wc -l)


PS3='Choose to restart options(input "all" for restart all and "q" to exit)> '

select pod in ${PODS};do

    if [[ "$REPLY" =~ ^[0-9]+$ ]] && [ "$REPLY" -ge 1 -a "$REPLY" -le $PODSNUM ];then
        echo -en "##########\\033[1;31m BEGIN \\033[0;39m##########";echo
        kubectl --server=${KUBESERVER} logs --tail=$LINES $pod --namespace=${NAMESPACE}

    elif [ "$REPLY" == 'q' ];then
        break
    else
        echo -en "##########\\033[1;31m BEGIN \\033[0;39m##########";echo
        echo 'Input error'
    fi

    REPLY=''
    echo;echo -en "##########\\033[1;31m  END  \\033[0;39m##########";echo
done
